package com.fu.mr.bluetoothdchat.thread;

import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothDevice;
import android.bluetooth.BluetoothServerSocket;
import android.bluetooth.BluetoothSocket;
import android.util.Log;

import java.io.IOException;
import java.lang.reflect.Method;
import java.util.UUID;

/**
 * <pre>
 *     author : Mr.Fu
 *     e-mail : 18622268981@163.com
 *     time   : 2020/03/13
 *     desc   : 监听外来蓝牙连接请求的线程
 *              准备接受新的连接.使用阻塞的方式,调用 BluetoothServerSocket.accept()
 *     version: 1.0
 * </pre>
 */

public class AcceptThread extends Thread {

    public static final String TAG = AcceptThread.class.getSimpleName();

    private static final String NAME_SECURE = "BluetoothChatSecure";
    private static final String NAME_INSECURE = "BluetoothChatInsecure";

    // UUID --> 通用唯一识别码
    private static final UUID MY_UUID = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");

    private final BluetoothAdapter mAdapter;

    private BluetoothServerSocket mServerSocket;

    private AcceptListener mListener;

    public AcceptThread(boolean secure) {

        Log.d(TAG, "AcceptThread -> secure is " + secure);
        mAdapter = BluetoothAdapter.getDefaultAdapter();
        BluetoothServerSocket tmp = null;
        try {
            if (secure) {
                tmp = mAdapter.listenUsingRfcommWithServiceRecord(NAME_SECURE, MY_UUID);
            } else {
                tmp = mAdapter.listenUsingInsecureRfcommWithServiceRecord(NAME_INSECURE, MY_UUID);
            }
        } catch (IOException e) {
            Log.d(TAG, "AcceptThread -> First failed");
            e.printStackTrace();
            Method listenMethod = null;
            try {
                listenMethod = mAdapter.getClass().getMethod(
                        "listenUsingRfcommOn", new Class[]{int.class});
                tmp = (BluetoothServerSocket) listenMethod.invoke(mAdapter, new Object[]{29});
            } catch (Exception e1) {
                Log.d(TAG, "AcceptThread -> Second failed");
                e1.printStackTrace();
            }
        }
        mServerSocket = tmp;
    }

    @Override
    public void run() {

        Log.d(TAG, "AcceptThread -> run()");
        BluetoothSocket socket = null;
        while (true) {
            try {
                if (mServerSocket != null) {
                    socket = mServerSocket.accept();
                }
            } catch (IOException e) {
                e.printStackTrace();
                break;
            }
            if (socket != null) {
                BluetoothDevice device = socket.getRemoteDevice();
                Log.d(TAG, "被动连接成功!");
                if (mListener != null) {
                    mListener.onAccept(socket, device);
                }
                break;
            } else {
                Log.d(TAG, "AcceptThread -> socket is null)");
            }
        }
    }

    public void cancel() {

        Log.w(TAG, "AcceptThread -> cancel()");
        if (mServerSocket != null) {
            try {
                mServerSocket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    public void setAcceptListener(AcceptListener listener) {

        mListener = listener;
    }

    public static interface AcceptListener {

        void onAccept(BluetoothSocket socket, BluetoothDevice device);
    }
}
